﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Out2Share
{
    partial class SharepointConnect
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("Out2Share.SharepointConnect")]
        public partial class SharepointConnectFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void SharepointConnectFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void SharepointConnect_FormRegionShowing(object sender, System.EventArgs e)
        {
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Controls.Add(ThisAddIn.treeViewContainer);

            // try to retrieve current folder
            try
            {
                Microsoft.Office.Tools.Outlook.FormRegionControl frc = (Microsoft.Office.Tools.Outlook.FormRegionControl)sender;
                object it = frc.OutlookItem;
                object Parent = it.GetType().InvokeMember("Parent", BindingFlags.GetProperty | BindingFlags.GetField, null, it, null);
                object FolderPath = Parent.GetType().InvokeMember("FolderPath", BindingFlags.GetProperty | BindingFlags.GetField, null, Parent, null);
                string folder = FolderPath.ToString();
                if (folder.LastIndexOf("\\") > 0)
                {
                    folder = folder.Substring(folder.LastIndexOf("\\") + 1);
                }
                ThisAddIn.treeViewContainer.currentFolder = folder;
            }
            catch (Exception) { }
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void SharepointConnect_FormRegionClosed(object sender, System.EventArgs e)
        {
            this.Controls.Clear();
        }
    }
}
